package cn.tedu.jsd2207csmall.passport.controller;

import cn.tedu.jsd2207csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.jsd2207csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.jsd2207csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.jsd2207csmall.passport.security.LoginPrincipal;
import cn.tedu.jsd2207csmall.passport.service.IAdminService;
import cn.tedu.jsd2207csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.PreDestroy;
import java.util.List;

@Api(tags = "01.管理员管理模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/admins")
public class AdminController {


    @Autowired
    IAdminService iAdminService;


    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.info("开始处理[管理员登录]的请求,参数:{}",adminLoginDTO);
        String jwt = iAdminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }


    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    public JsonResult<Void> AddNew(AdminAddNewDTO adminAddNewDTO){
        log.info("开始处理[添加管理员请求]参数:{}",adminAddNewDTO);
        iAdminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @ApiOperation("管理员列表")
    public JsonResult<AdminListItemVO> list(
          @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal
    ){
        log.info("开始处理管理员列表的请求");
        log.info("当前登录的当事人:{}",loginPrincipal);
       List<AdminListItemVO> list= iAdminService.list();
       return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperationSupport(order=200)
    @PreAuthorize("hasAnyAuthority('/ams/admin/delete')")
    @ApiImplicitParam(name="id",value = "管理员id",required = true,dataType = "long")
    @ApiOperation("删除管理员")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除相册失败,尝试删除的相册ID无效")
            @PathVariable Long id){
        log.info("执行删除id为:{}的管理员的请求",id);
        iAdminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperationSupport(order=310)
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    @ApiImplicitParam(name="id",value = "管理员id",required = true,dataType = "long")
    @ApiOperation("启用管理员")
    public JsonResult<Void> setEnable(@Range(min = 1,message = "启用失败,尝试启用的ID无效")
                                   @PathVariable Long id){
        log.info("执行启用id为:{}的管理员的请求",id);
        iAdminService.serEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disenable")
    @ApiOperationSupport(order=311)
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    @ApiImplicitParam(name="id",value = "管理员id",required = true,dataType = "long")
    @ApiOperation("禁用管理员")
    public JsonResult<Void> setDisEnable(@Range(min = 1,message = "禁用失败,尝试禁用的ID无效")
                                      @PathVariable Long id){
        log.info("执行禁用id为:{}的管理员的请求",id);
        iAdminService.serDisEnable(id);
        return JsonResult.ok();
    }



}
